package cn.zs.supply.common.utils;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * 删除excel表单列操作
 */
public class ExcelCellDelete {
	//合并的单元格的信息
	int firstColumn;int lastColumn;int firstRow;int lastRow;int rangeNum;Sheet sheet;
	//操作过的合并单元格
	String operatedRanges="";
	//起始行
	int firstRowNum=1;
	
	
	
	public ExcelCellDelete(Sheet sheet){
		this.sheet=sheet;	
	}
	/**
     * 删除列
     * @param sheet
     * @param columnToDelete
     */
    public void deleteColumn(int columnToDelete) {
       for (int rId = firstRowNum; rId <= sheet.getLastRowNum(); rId++) {
            Row row = sheet.getRow(rId);
            for (int cID = columnToDelete; cID <= row.getLastCellNum(); cID++) {
            	//判断是否合并单元格
            	if(!isMergedRegion(rId ,cID)) { 
                Cell cOld = row.getCell(cID);             
                if (cOld != null) {
                    row.removeCell(cOld);
                }
                Cell cNext = row.getCell(cID + 1);
                if (cNext != null) {
                	//判断下一列是否合并单元格
                	boolean b=false;
                	if(isMergedRegion(rId ,cID+1)) {
                		b=true;
                	}
                    Cell cNew = row.createCell(cID, cNext.getCellType());
                    cloneCell(cNew, cNext);
                    if (rId == 0) {
                        sheet.setColumnWidth(cID, sheet.getColumnWidth(cID + 1));
                    }
                    if(b) {
                    	firstColumn=firstColumn-1;lastColumn=lastColumn-1;
                    	setMergedRegion();
                    }
                }
                }
            	else {    
            		lastColumn=lastColumn-1;
            		setMergedRegion();
            	}
            }
        }
    }
    /**
     * 判断指定的单元格是否是合并单元格
     * @param sheet
     * @param row 行下标
     * @param column 列下标
     * @return
     */
    private  boolean isMergedRegion(int row ,int column) {
        int sheetMergeCount = sheet.getNumMergedRegions();
        for (int i = 0; i < sheetMergeCount; i++) {
            CellRangeAddress range = sheet.getMergedRegion(i);
            int firstColumn = range.getFirstColumn();
            int lastColumn = range.getLastColumn();
            int firstRow = range.getFirstRow();
            int lastRow = range.getLastRow();
            if(row >= firstRow && row <= lastRow){
                if(column >= firstColumn && column <= lastColumn){
                	this.firstColumn=firstColumn;this.lastColumn=lastColumn;this.firstRow=firstRow;this.lastRow=lastRow;
                	rangeNum=i;
                    return true;
                }
            }
        }
        return false;
    }
    /**
     * 缩减合并的单元格
     */
    private  void  setMergedRegion() {
        try {
        	        if(!operatedRanges.contains(rangeNum+"")) {
        	        operatedRanges+=rangeNum+"|";
                	sheet.removeMergedRegion(rangeNum);
                	CellRangeAddress ranges = new CellRangeAddress(firstRow, lastRow,firstColumn, lastColumn);
                    sheet.addMergedRegion(ranges);	
        	        }
        }
        catch (Exception e) {
			// TODO: handle exception
		}
    }
    /**
     * 右边列左移
     * @param cNew
     * @param cOld
     */
    private void cloneCell(Cell cNew, Cell cOld) {
        cNew.setCellComment(cOld.getCellComment());
        cNew.setCellStyle(cOld.getCellStyle());
        cNew.setCellValue(ExeclUtil.getCellValue(cOld, true).trim());
    }
	public int getFirstRowNum() {
		return firstRowNum;
	}
	public void setFirstRowNum(int firstRowNum) {
		this.firstRowNum = firstRowNum;
	}
   
}